<?php

class UserLoginModel
{
    private $userLoginKeyPrefix =   'UL/';
   
    public function initUserLogin($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['account']) ||  empty($info['passwd']))
        {
            AC::$errorNo    =   'ULM001';
            return  FALSE;
        }

        $key    =   $this->userLoginKeyPrefix . $info['account'];
        $data   =   array(
                        'uid'       =>  $info['uid'],
                        'account'   =>  $info['account'],
                        'passwd'    =>  UK::cryptPasswd($info['account'], $info['passwd']),
                        'nickname'  =>  $info['nickname'],
                        'loginTime' =>  AC::SERVER_TIME,
                        'loginIp'   =>  UK::getClientIp(),
                        'status'    =>  AC::REGISTER_DEFAULT_STATUS,
                    );

        isset($info['image'])       &&  $data['image']      =   $info['image'];
	isset($info['type'])        &&  $data['type']       =  intval($info['type']);

        $result =   Storage::addData($key, $data);
        if (empty($result))
        {
            AC::$errorNo    =   'ULM002';
            return  FALSE;
        }
        return  TRUE;
    }/*}}}*/

    public function userLogin($info)/*{{{*/
    {
        if (empty($info['account']) ||  empty($info['passwd']))
        {
            AC::$errorNo    =   'ULM003';
            return  FALSE;
        }

        $key    =   $this->userLoginKeyPrefix. $info['account'];
        # $where  =   array('passwd' => UK::cryptPasswd($info['account'], $info['passwd']));
        # $info   =   Storage::getConditionData($key, $where);

        $info   =   $this->getUserInfo($info);
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'ULM004';
            return  FALSE;
        }
        
        if (!empty($info['status']))
        {
            AC::$errorNo    =   'ULM005';
            return  FALSE;
        }
        unset($info['id']);
        //unset($info['account']);
        unset($info['passwd']);

        $value  =   array(
                        'loginIp'   =>  UK::getClientIp(),
                        'loginTime' =>  AC::SERVER_TIME,
                    );
        $autoInc=   array(
                        'loginTimes'=>  1,
                    );
        $data   =   array('value' => $value, 'autoInc' => $autoInc);
        $result =   Storage::complexUpdate($key, $data);

        return  $info;
    }/*}}}*/
    
    /*
     * 根据帐号获取信息
    * @param unknown $info
    * @return Ambigous <multitype:, unknown, boolean, mixed>
    */
    public function getUserInfoByAccount($account)/*{{{*/
    {
        if (empty($account)) return array();

    	$key    =   $this->userLoginKeyPrefix. $account;    
    	$where  =   array('account'=> $account);
    	$info   =   Storage::getConditionData($key, $where);    
    	return  $info;
    }/*}}}*/
    
    public function delUserInfoByAccount($account,$uid)/*{{{*/
    {    	 
    	$key    =   $this->userLoginKeyPrefix. $account;
    	$where  =   array('account'=> $account,'uid'=>$uid);    	 
    	$info   =   Storage::delConditionData($key, $where);
    	return  $info;
    }/*}}}*/
       
    public function updateLoginPasswd($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['account']) ||  empty($info['oldPasswd'])   ||  empty($info['newPasswd']))
        {
            AC::$errorNo    =   'ULM007';
            return  FALSE;
        }
        if ($info['oldPasswd']  ==  $info['newPasswd'])
        {
            AC::$errorNo    =   'ULM014';
            return  FALSE;
        }
        $info['passwd'] =   $info['oldPasswd'];
        $loginInfo      =   $this->getUserInfo($info);
        if (empty($loginInfo['uid']))
        {
            AC::$errorNo    =   'ULM008';
            return  FALSE;
        }

        $key    =   $this->userLoginKeyPrefix.  $info['account'];
        $data   =   array('passwd' => UK::cryptPasswd($info['account'], $info['newPasswd']));
        $where  =   array('uid' => $info['uid'], 'passwd' => UK::cryptPasswd($info['account'], $info['oldPasswd']));
        $result =   Storage::setConditionData($key, $data, $where);

        if (empty($result)) AC::$errorNo    =   'ULM015';

        return  $result;
    }/*}}}*/

    public function setUserInfo($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['account']))
        {
            AC::$errorNo    =   'ULM009';
            return  FALSE;
        }
        
        $key    =   $this->userLoginKeyPrefix . $info['account'];

        isset($info['nickname'])    &&  $data['nickname']   =   $info['nickname'];
        isset($info['status'])      &&  $data['status']     =   $info['status'];
        if (isset($info['image'])){
            $data['image'] =   AC::SERVER_TIME;
        }

        if (empty($data))   return  TRUE;

        $where  =   array('account' => $info['account']);
        $result =   Storage::setConditionData($key, $data, $where);

        if (empty($result)) AC::$errorNo    =   'ULM010';
        
        return  $result;
    }/*}}}*/
    
    /**
     * 重置密码
     * @param unknown $info
     * @return boolean
     */
    public function resetPasswd($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['account']) ||  empty($info['email']))
        {
            AC::$errorNo    =   'ULM011';
            return  FALSE;
        }

        $makePasswd =   UK::makeRandPasswd();

        // 发邮件
        $res_email = $this->sendEmail($info['email'], $info['account'], $makePasswd);
        if (!$res_email) return false;
        
        // 修改密码
        $result = $this->updateAccountLoginPasswd($info['uid'], $info['account'], md5($makePasswd));
        if (!$result) return false;
        
        return true;
    }/*}}}*/
    
    /**
     * 发邮件
     * @param unknown $email
     * @param unknown $account
     * @param unknown $makePasswd
     * @return boolean
     */
    public function sendEmail ($email, $account, $makePasswd) {
        $subject    =   '粉粉账号找回密码';
        $body       =
        '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>粉粉账号找回密码</title>
</head>
<body>
    <h1><span>粉粉账户:</span>'. $account. '</h1>
    <h1><span>重置密码:</span>'. $makePasswd. '</h1>
    <p>请使用上面的密码登录粉粉日记，然后修改为您自己的密码。</p>
    <p></p>
    <p>更多信息，请随时关注：<br />粉粉日记官方网站：https://www.fenfenriji.com </p>
    <p></p>
    <p>本邮件是根据用户的请求自动发送，不接收任何邮件回复，有问题请联系上面的信息</p>
</body>
</html>';
        
        $tmp = array(
            'email'     =>  $email,
            'subject'   =>  $subject,
            'body'      =>  $body
        );

        $result =   UK::ffrjEmail($tmp);
        if (empty($result)) AC::$errorNo    =   'ULM013';
        
        return  $result;
    }

    public function sendEmailCode ($email, $code) {
        $subject    =   '粉粉账号密码修改邮箱验证码';
        $body       =
            '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>粉粉账号找回密码</title>
</head>
<body>
    <h1><span>验证码:</span>'. $code. '</h1>
    <p>请使用上面的验证码继续找回密码的操作。验证码有效期30分钟。</p>
    <p></p>
    <p>更多信息，请随时关注：<br />粉粉日记官方网站：https://www.fenfenriji.com </p>
    <p></p>
    <p>本邮件是根据用户的请求自动发送，不接收任何邮件回复，有问题请联系上面的信息</p>
</body>
</html>';

        $tmp = array(
            'email'     =>  $email,
            'subject'   =>  $subject,
            'body'      =>  $body
        );

        $result =   UK::ffrjEmail($tmp);
        if (empty($result)) AC::$errorNo    =   'ULM013';

        return  $result;
    }

    public function sendEmailUserAccount ($email, $username) {
        $subject    =   '粉粉账号邮箱找回账号名';
        $body       =
            '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>粉粉账号找回密码</title>
</head>
<body>
            <h1><span>粉粉账号名:</span>'. $username. '</h1>
    <p>快使用上面的粉粉账号名登录你的粉粉日记吧。</p>
    <p></p>
    <p>更多信息，请随时关注：<br />粉粉日记官方网站：https://www.fenfenriji.com </p>
    <p></p>
    <p>本邮件是根据用户的请求自动发送，不接收任何邮件回复，有问题请联系上面的信息</p>
</body>
</html>';

        $tmp = array(
            'email'     =>  $email,
            'subject'   =>  $subject,
            'body'      =>  $body
        );

        $result =   UK::ffrjEmail($tmp);
        if (empty($result)) AC::$errorNo    =   'ULM013';

        return  $result;
    }
    
    public function getUserInfo($info)/*{{{*/
    {
        $key    =   $this->userLoginKeyPrefix. $info['account'];
        $where  =   array('passwd' => UK::cryptPasswd($info['account'], $info['passwd']));
        $info   =   Storage::getConditionData($key, $where);

        return  $info;
    }/*}}}*/

    // 获取用户头像
    public function getUserImage($info){
        $key    =   $this->userLoginKeyPrefix. $info['account'];
        $where  =   array('account' => $info['account']);
        $info   =   Storage::getConditionData($key, $where);
        
        return  $info;
    }
    
    /**
     * 修改用户登陆密码
     * @param unknown $uid
     * @param unknown $account
     * @param unknown $newPasswd
     * @return Ambigous <boolean, unknown, number, resource>
     */
    public function updateAccountLoginPasswd ($uid, $account, $newPasswd, $oldPasswd = '') {
        if (empty($uid) || empty($account)) return false;
        
        $key    =   $this->userLoginKeyPrefix. $account;
        $data   =   array('passwd' =>  UK::cryptPasswd($account, $newPasswd));
        $where  =   array('account' => $account, 'uid' => $uid);
        if ($oldPasswd) {
            $where  =   array('account' => $account, 'passwd' => UK::cryptPasswd($account, $oldPasswd));
            $result = Storage::getConditionData($key, $where);
            if (empty($result)) {
                AC::$errorNo    =   'ULM012';
                return false;
            }
        }
        $result =   Storage::setConditionData($key, $data, $where);
    
        if (empty($result)) AC::$errorNo    =   'ULM012';
        return $result;
    }
}

